﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.AddressableAssets.Build.AnalyzeRules;
using UnityEditor.AddressableAssets.GUI;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
// Analyze自定义规则
public class MyAddressableRule : AnalyzeRule
{
    public override bool CanFix { get { return true; } }
    public override string ruleName
    {
        get { return "MyRule"; }
    }


    public override List<AnalyzeResult> RefreshAnalysis(AddressableAssetSettings settings)
    {
        List<AnalyzeResult> results = new List<AnalyzeResult>();

        foreach(var group in settings.groups)
        {
            foreach(var asset in group.entries)
            {
                if (asset.address.StartsWith("NPC2"))
                {
                    AnalyzeResult t = new AnalyzeResult();
                    t.resultName = group.name + kDelimiter + asset.address;
                    t.severity = UnityEditor.MessageType.Warning;

                    results.Add(t);
                }
            }
        }
        return results;
    }
    public override void FixIssues(AddressableAssetSettings settings)
    {
        
        foreach(var group in settings.groups)
        {
            foreach(var asset in group.entries)
            {
                if (asset.address.StartsWith("NPC2"))
                {
                    asset.address = "NPC";
                }
            }
        }
    }

}

//注册到界面上
[InitializeOnLoad]
class RegisterMyRule
{
    static RegisterMyRule()
    {
        AnalyzeWindow.RegisterNewRule<MyAddressableRule>();
    }
}